package com.j1903.controller;

import com.j1903.bean.Goodscategory;
import com.j1903.util.JsonResult;
import com.j1903.service.GoodscategoryService;

import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.j1903.vo.TopCateGroup;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 分类表 前端控制器
 * </p>
 *
 * @author fyq
 * @since 2021-12-13
 */
@RestController
@RequestMapping("/goodscategory")
public class GoodscategoryController {
        @Resource
        private GoodscategoryService goodscategoryService;
        @RequestMapping("/add")
        public JsonResult add(@RequestBody Goodscategory obj){
                goodscategoryService.save(obj);
                return new JsonResult(200,"添加成功",
                        obj.getId());
        }
        @RequestMapping("/upd")
        public JsonResult upd(@RequestBody Goodscategory obj){
                Boolean rs=goodscategoryService.saveOrUpdate(obj);
                if(rs){
                        return new JsonResult(200,"更新成功");
                }
                else{
                        return new JsonResult(500,"更新失败");
                }
        }
        @RequestMapping("/delById")
        public JsonResult delById(Long id){
                Boolean rs=goodscategoryService.removeById(id);
                if(rs){
                        return new JsonResult(200,"删除成功");
                }
                else{
                        return new JsonResult(500,"删除失败");
                }
        }
        @RequestMapping("/list")
        public JsonResult list(@RequestBody Goodscategory obj){
                List<Goodscategory> list=goodscategoryService.lists(obj);
                return new JsonResult(200,"查询成功",list);
        }
        @RequestMapping("/page/{curPage}/{pageRecords}")
        public JsonResult page(@RequestBody Goodscategory obj,
                               @PathVariable int curPage,
                               @PathVariable int pageRecords){
                IPage<Goodscategory> ipage=new Page<>(curPage,pageRecords);
                IPage<Goodscategory> page=goodscategoryService.pages(ipage,obj);
                return new JsonResult(200,"查询分页成功",page);
        }
        @RequestMapping("/getTopGroup/{topId}")
        public JsonResult getTopGroup(
                @PathVariable("topId") Long topId){
                Map<String,List<TopCateGroup>> map=
                        (Map<String, List<TopCateGroup>>) goodscategoryService.getWithSub(
                                new Goodscategory(topId));
                return new JsonResult(200,"查询顶级分类所属商品和分类成功",map);
        }
}

